#include "loginwidget.h"

#include<QPixmap>
#include<QProcess>
#include<QLabel>
#include<QSpacerItem>
#include<QHBoxLayout>
#include<QVBoxLayout>
#include<QLineEdit>
#include<QPushButton>
#include<QMessageBox>
#include<QDateTime>
#include<QThread>

#include<QCryptographicHash>
#include <QFile>
#include <QFileInfo>
#include<QResource>
#include<QMessageBox>
#include"comdef.h"
#define MYLOG qDebug() << "[" << __FILE__ << ":" << __LINE__ << ":" << QThread::currentThread() << __func__ << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz")<< "]"
LoginWidget::LoginWidget(QWidget *parent) :
  QWidget(parent)
{
  create();

}

LoginWidget::~LoginWidget()
{
  if(mainWindow != nullptr)
    {
      delete mainWindow;
    }
  mainWindow = nullptr;
}




void LoginWidget::create()
{
  this->setWindowTitle("用户登录");
  setAttribute(Qt::WA_DeleteOnClose,false);

  this->setWindowIcon(QIcon(":/source/user.jpg"));
  this->setFixedSize(this->width(),this->height());
  this->setStyleSheet("background-color: white;");

  //加载图片
  loginImage.load(":/source/Login.jpg");
  QLabel *label_logo = new QLabel(this);
  label_logo->setFixedSize(160,160);

  label_logo->setPixmap(QPixmap::fromImage(loginImage));

  QLabel *label_register = new QLabel("注册账号");
  label_register->setStyleSheet("font-family: 微软雅黑;");
  label_register->setAlignment(Qt::AlignRight);

  lineEdit_username = new QLineEdit(this);
  lineEdit_username->setPlaceholderText("username");
  lineEdit_username->setAlignment(Qt::AlignCenter);

  lineEdit_password = new QLineEdit(this);
  lineEdit_password->setPlaceholderText("password");
  lineEdit_password->setAlignment(Qt::AlignCenter);

  lineEdit_username->setFixedSize(280,35);
  lineEdit_username->setStyleSheet("QLineEdit{width: 100px;height: 100px;font-family: 微软雅黑;font-size: 18px;border: 1px solid green;border-radius: 15px;}");
  lineEdit_password->setFixedSize(280,35);
  lineEdit_password->setStyleSheet("QLineEdit{width: 100px;height: 100px;font-family: 微软雅黑;font-size: 18px;border: 1px solid green;border-radius: 15px;}");

  QPushButton *pushButton_login = new QPushButton("登录");
  pushButton_login->setFixedSize(280,35);
  pushButton_login->setStyleSheet("QPushButton{width: 100px;height: 100px;border: 1px solid green;font-family: 微软雅黑;border-radius: 15px;font-size: 18px;background-color: #00ffff}""QPushButton:pressed{background:white;}");



  //输入密码
  lineEdit_username->setText("admin");
  lineEdit_password->setText("123456");

  //弹簧
  QSpacerItem *h_spacer = new QSpacerItem(10,10,QSizePolicy::Expanding,QSizePolicy::Minimum);
  QSpacerItem *v_spacer = new QSpacerItem(10,10,QSizePolicy::Minimum,QSizePolicy::Expanding);

  QHBoxLayout *h_layout_1 = new QHBoxLayout;
  h_layout_1->addItem(h_spacer);
  h_layout_1->addWidget(label_logo);
  h_layout_1->addItem(h_spacer);

  QVBoxLayout *v_layout_1 = new QVBoxLayout;
  v_layout_1->addItem(v_spacer);
  v_layout_1->addWidget(lineEdit_username);
  v_layout_1->addItem(v_spacer);
  v_layout_1->addWidget(lineEdit_password);
  v_layout_1->addItem(v_spacer);
  v_layout_1->addWidget(pushButton_login);
  v_layout_1->addItem(v_spacer);

  QHBoxLayout *h_layout_2 = new QHBoxLayout;
  h_layout_2->addItem(h_spacer);
  h_layout_2->addLayout(v_layout_1);
  h_layout_2->addItem(h_spacer);

  QVBoxLayout *v_layout_2 = new QVBoxLayout(this);

  v_layout_2->addLayout(h_layout_1);
  v_layout_2->addLayout(h_layout_2);
  v_layout_2->addWidget(label_register);

  setLayout(v_layout_2);

  connect(pushButton_login,SIGNAL(clicked()),this,SLOT(login_clicked()));
}

bool LoginWidget::ReadUuid()
{
  // 创建进程对象
  QProcess process;

  // 启动进程
  process.start("cmd.exe", QStringList() << "/C" << "wmic csproduct get UUID");

  // 等待进程结束
  if (!process.waitForFinished()) {
      MYLOG << "Failed to execute command";
    }

  // 获取命令行输出结果
  QString result = QString(process.readAllStandardOutput());

  result.replace("\r","");
  result.replace("\n","");
  result.replace(" ","");

  QString uuid = result.right(result.size() - 4);

  // 使用 SHA-256 算法对 UUID 进行哈希加密
  QByteArray hashResult = QCryptographicHash::hash(uuid.toUtf8(), QCryptographicHash::Sha256);

  QString uniqueId;
  for (int i = 0; i < hashResult.size(); ++i) {
      QString byteString = QString::number(hashResult.at(i), 2);
      byteString = byteString.rightJustified(8, '0'); // 将二进制字符串补齐至8位
      uniqueId += byteString;
  }

  //  MYLOG<<uniqueId;
  QString filename = QApplication::applicationDirPath() + "/License.txt";
  QString hashUuid;

  if (QFile::exists(filename)) {
      QFile file(filename);

      if (!file.open(QIODevice::ReadOnly)) {
          MYLOG<<"Can't open the file!";
          return false;
        }else{
          hashUuid = file.readAll();
//           MYLOG<<hashUuid;
          file.close();
        }

    }else{
      MYLOG<<"file is not exists!";
      return false;
    }

  if(hashUuid == uniqueId){
//      MYLOG<<"uid ture!";
      return true;
    }else{
      return false;
    }

}

void LoginWidget::login_clicked()
{
  //获取密码
  username = lineEdit_username->text();
  password = lineEdit_password->text();

  if(username == "admin" && password == "123456")
    {
      try{
        if(ReadUuid())
          {
            qDebug()<<"uid1231546 true";
            //主界面
            mainWindow = new MainWindow();
            mainWindow->show();
            this->hide();
          }else{
            QMessageBox::warning(this,tr("提示"),tr("未获得许可!\n请联系生产商获得许可!"),QMessageBox::Ok);
            this->close();
          }
      }
      catch(std::exception &ex)
      {
        MYLOG<<ex.what();
        qWarning()<<"LoginWidget"<<ex.what();
        return ;
      }
    }
  else{
      QMessageBox::critical(this,tr("提示"),tr("用户名或密码有误!"),QMessageBox::Ok);
    }
}


